package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrResqVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品属性
 *
 * @author xiaolin
 * @email 956704533@qq.com
 * @date 2020-06-08 22:10:40
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * /product/attr/update/{spuId}
     * 修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R updateAttrValue(@PathVariable("spuId") Long spuId,
                             @RequestBody List<ProductAttrValueEntity> valueEntities){
        productAttrValueService.updateAttrValue(spuId,valueEntities);
        return R.ok();
    }

    /**
     * /product/attr/base/listforspu/{spuId}
     * 获取spu规格参数
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entitis=productAttrValueService.baseAttrListforspu(spuId);
        return R.ok().put("data",entitis);
    }

    /**
     * /product/attr/base/list/{catelogId}
     * /product/attr/sale/list/{catelogId}
     * 获取规格参数基本信息或者是销售属性信息，两个方法共用
     *  注意事项：
     *      只有规格参数才有属性分组，销售属性是没有分组的，所以在新增或修改销售属性时，无需关联属性与属性分组的中间表
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R queryBaseAttrPage(@RequestParam Map<String,Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String type){
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId,type);
        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查询属性详情
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrResqVo resqVo =attrService.getAttrInfo(attrId);
        return R.ok().put("attr", resqVo);
    }

    /**
     * 保存属性信息与其关联关系
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改属性和关联表的信息
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
